package csvreader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import data.Data;

public class CSVParser {

	public void parseFile(String filename, Data data){
		try {
			System.out.println("CSVParser: Started on time - " + GregorianCalendar.getInstance().getTime().toString());
			FileReader fr = new FileReader(filename);
			BufferedReader br = new BufferedReader(fr);
			boolean firsttime = true; //to insert TimeUnits one time

			String kbread = br.readLine();
			String kbwrite = br.readLine();
			String numread = br.readLine();
			String numwrite = br.readLine();

			//		StringTokenizer kbrTokens = new StringTokenizer(kbread, ",");
			//		System.out.println(kbread);
			int counter = 0;
			while (kbread != null){
				//each tokenizer holds this info for a Lun in all time units
				StringTokenizer kbrTokens = new StringTokenizer(kbread, ",");
				StringTokenizer kbwTokens = new StringTokenizer(kbwrite, ",");
				StringTokenizer numrTokens = new StringTokenizer(numread, ",");
				StringTokenizer numwTokens = new StringTokenizer(numwrite, ",");

				//the first column is not relevant (machine name)
				kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();
				//the second column is not relevant (LUN name)
				kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();
				//the third column is not relevant (field name)
				kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();
				//the 4th column is not relevant (weird number)
				kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();

				//the 5th column is not relevant (day of the week)
				kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();

				//the 6th column is the start date without year (the same at all fields)
				String sdate=kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();

				//the 7th column is the start date year (the same at all fields)
				String syear=kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();

				//the 8th column is not relevant (day of the week)
				kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();

				//the 9th column is the end date without year (the same at all fields)
				/*String edate=*/kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();

				//the 10th column is the end date year (the same at all fields)
				/*String eyear=*/kbrTokens.nextToken();
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();

				//the 11th column is the time unit size (the same at all fields)
				int tuSize=Integer.parseInt(kbrTokens.nextToken());
				kbwTokens.nextToken();
				numrTokens.nextToken();
				numwTokens.nextToken();

				Calendar sd =computeDate(sdate, syear);
				
				//sd.add(Calendar.SECOND, 600);
				//System.out.println(sd.get(Calendar.DAY_OF_MONTH)+"."+(sd.get(Calendar.MONTH)+1)+"."+sd.get(Calendar.YEAR)+" ,"+sd.get(Calendar.HOUR_OF_DAY)+":"+sd.get(Calendar.MINUTE)+":"+sd.get(Calendar.SECOND));
//				Calendar ed =computeDate(edate, eyear);

				//TODO: change the start and end time according to the size of each time unit
				data.addLun(counter, 10000000);
				int tucount=0;
				

				while(kbrTokens.hasMoreTokens()){
					if (firsttime){
						Calendar d = (GregorianCalendar)sd.clone();
						sd.add(Calendar.SECOND, tuSize);						
						data.addTimeUnit(tucount, tuSize, d);
					}
										
					StringTokenizer st = new StringTokenizer(kbrTokens.nextToken(),"\"");
					double kbr = Double.parseDouble(st.nextToken())*tuSize;
					st = new StringTokenizer(kbwTokens.nextToken(),"\"");
					double kbw = Double.parseDouble(st.nextToken())*tuSize;
					st = new StringTokenizer(numrTokens.nextToken(),"\"");
					double numr = Double.parseDouble(st.nextToken())*tuSize;
					st = new StringTokenizer(numwTokens.nextToken(),"\"");
					double numw = Double.parseDouble(st.nextToken())*tuSize;


					data.addLunPerTimeUnit(counter, tucount, kbw, kbr, numr, numw);


					tucount++;

				}//end of while


				//reading for the next iteration
				kbread = br.readLine();
				kbwrite = br.readLine();
				numread = br.readLine();
				numwrite = br.readLine();
				firsttime = false;
//				System.out.println("CSVParser: Done with LUN - " + counter);
				counter++;
			}//end of while
			data.endTransactions();
			System.out.println("CSVParser: Ended on time: " + GregorianCalendar.getInstance().getTime().toString());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}


	private Calendar computeDate(String date, String year) {
		StringTokenizer tmp = new StringTokenizer(year," \"");
		StringTokenizer tokens = new StringTokenizer(date);
		String month = tokens.nextToken();
		int day = Integer.parseInt(tokens.nextToken());
		int iyear = Integer.parseInt(tmp.nextToken());
		int imonth = getMonth(month);
		GregorianCalendar ans = new GregorianCalendar(iyear, imonth, day);
		return ans;
	}


	private int getMonth(String month) {
		if(month.equals("January"))
			return Calendar.JANUARY;
		else
			if(month.equals("February"))
				return Calendar.FEBRUARY;
			else
				if(month.equals("March"))
					return Calendar.MARCH;
				else
					if(month.equals("April"))
						return Calendar.APRIL;
					else
						if(month.equals("May"))
							return Calendar.MAY;
						else
							if(month.equals("June"))
								return Calendar.JUNE;
							else
								if(month.equals("July"))
									return Calendar.JULY;
								else
									if(month.equals("August"))
										return Calendar.AUGUST;
									else
										if(month.equals("September"))
											return Calendar.SEPTEMBER;
										else
											if(month.equals("October"))
												return Calendar.OCTOBER;
											else
												if(month.equals("November"))
													return Calendar.NOVEMBER;
												else return Calendar.DECEMBER;

	}


	public static void main(String[] args){
				CSVParser cr = new CSVParser();
		//		cr.parseFile("csv example.csv", new SQLiteDB("csv_example.db"));
		Calendar sd =cr.computeDate("Dec 28", "2006");
		for(int i=0; i<1000; i++){
			Calendar d = (GregorianCalendar)sd.clone();
			sd.add(Calendar.SECOND, 600);
			System.out.println(d.get(Calendar.DAY_OF_MONTH)+"."+(d.get(Calendar.MONTH)+1)+"."+d.get(Calendar.YEAR)+" ,"+d.get(Calendar.HOUR_OF_DAY)+":"+d.get(Calendar.MINUTE)+":"+d.get(Calendar.SECOND));
		}
		
	}

}
